//通用方法文件
cc.Class({
    extends: cc.Component,

    properties: {

    },

    //初始化节点左右上下[四角]的真实位置对象
    Init_React(to,bo,le,ri) {
        var point = {
            left: le | 0,
            right: ri | 0,
            top: to | 0,
            bottom: bo | 0
        }
        return point;
    },

    //创建虚拟原点
    Create_Origin (screen) {
        var width = screen.width * 0.5;
        var height = screen.height * 0.5;
        var origin = cc.p(width,height);
        return origin;
    },

    //角度转弧度制
    Transform_Angel(angle) {
        var radius = angle * Math.PI / 180;
        radius = parseFloat(radius.toFixed(4));
        return radius;
    },
    
    //弧度转角度
    Transform_Radius(radius) {
        var angle = radius / (Math.PI / 180);
        angle = parseFloat(angle.toFixed(4));
        return angle; 
    },

    //幂运算
    //sum:幂运算次数[n的几次方] base:幂运算基数
    Power (sum,base) {
        var num = 1;
        for(var i = 0; i < sum; i++) {
            num = num * base;
        }
        return num;
    },

    //更换精灵帧
    //node:需要更换的节点 frame:精灵帧信息
    Change_Frame(node,frame) {
        node.getComponent(cc.Sprite).spriteFrame = frame;
    },

    //节点方向判断[返回的是参照点位于判断节点的位置]
    //node:判断节点 refer:参照点 dir:要判断的方向[左右还是上下]
    Direction_Check(node,refer,dir) {
        if(dir == "x") {
            if(node.x < refer.x) {
                return "right";
            }else {
                return "left";
            }
        }else if(dir == "y") {
            if(node.y < refer.y) {
                return "top";
            }else {
                return "bottom";
            }
        }
    },

    //返回两点间距离
    //posA: 点A posB: 点B
    Distance_Point(posA,posB) {
        var x_dis = posA.x - posB.x;
        var y_dis = posA.y - posB.y;
        var sum = (x_dis * x_dis) + (y_dis * y_dis)
        var dis = Math.abs(Math.sqrt(sum));
        return parseInt(dis);
    },
    
    //返回矢量移动偏移量
    //angel:角  speed:移动速度
    Return_Vector (angel,speed) {
        var move_pos = cc.p(0,0);
        var radius = this.Transform_Angel(angel)
        var x_pos = Math.cos(radius) * speed;
        var y_pos = Math.sin(radius) * speed;
        x_pos = parseFloat(x_pos.toFixed(4));
        y_pos = parseFloat(y_pos.toFixed(4));
        move_pos = cc.p(x_pos,y_pos);
        return move_pos;
    },

    //返回某个区间的随机数
    //p_one: 点一 p_two: 点二
    Random_Section (p_one,p_two) {
        var left = 0;
        var right = 0;
        var distance = 0;
        var r_left = 0;
        var r_right = 0;
        var seed = Math.random();
        //判断两点大小确定左右边界[left小 right大]
        if (p_one > p_two) {
            left = p_two;
            right = p_one;
        }else if(p_one < p_two){
            left = p_one;
            right = p_two; 
        }else {
            //若两点相等返回false
            return false;
        }
        //若两边的绝对值相等则表示一正一负
        if(Math.abs(right) == Math.abs(left)) {
            distance = Math.abs(left);
            r_left = left + Math.abs(parseInt((distance * seed)));
            distance = Math.abs(right);
            r_right = right - Math.abs(parseInt((distance * seed)));
        }else {
            distance = Math.abs(right) - Math.abs(left);
            r_left = Math.abs(left) + Math.abs(parseInt((distance * seed)));
            r_right = Math.abs(right) - Math.abs(parseInt((distance * seed)));
            //若某一点为原为负数则转换为负数
            left < 0 ? r_left = r_left * (-1) : r_left;
            right < 0 ? r_right = r_right * (-1) : r_right;
        }
        if(parseInt(Math.random() * 10) % 2 == 0) {
            return r_left;
        }else {
            return r_right;
        }
    },

    //边界判定[是否超出屏幕]
    //screen：屏幕对象 node:要判断的节点
    Check_Screen (screen,node) {
        var node_w = node.width;
        var node_y = node.height;
        var width = screen.width;
        var height = screen.height;
        var anchor = {x: node.anchorX, y: node.anchorY}
        var react = this.Init_React(0,0,0,0);
        react.left = node.x - node_w * anchor.x;
        react.right = node.x + node_w * anchor.x;
        react.top = node.y - node_y * anchor.y;
        react.bottom = node.y + node_y * anchor.y;
        //若在区域内返回false
        //这里乘上屏幕锚点是为了确定当前的(0,0)点位置,此时node相对于屏幕原点的x左边为负数,y的上边为负数
        if (react.left > -(width * screen.anchor.x) && react.right < width) {
            if(react.top > -(height * screen.anchor.y) && react.bottom < height) {
                return false;
            }else {
                return true;
            }
        }else {
            return true;
        }
    },

    //返回角度[余弦定理]
    //p_a,p_b,p_c: 三点位置 type:需要返回的某个位置角[角a、b、c]
    Return_Angel (p_a,p_b,p_c,type) {
        var angle = 0;
        var numerator = 0;
        var denominator = 0;
        var cosA = 0; var cosB = 0; var cosC = 0;
        var dis_a = this.Distance_Point(p_a,p_b);
        var dis_b = this.Distance_Point(p_b,p_c);
        var dis_c = this.Distance_Point(p_c,p_a);
        switch(type) {
            case "a":
            case "A":
                numerator = this.Power(2,dis_b) + this.Power(2,dis_c) - this.Power(2,dis_a);
                denominator = 2 * dis_b * dis_c;
                cosA = numerator / denominator;
                angle = Math.acos(cosA);
            break;
            case "b":
            case "B":
                numerator = this.Power(2,dis_c) + this.Power(2,dis_a) - this.Power(2,dis_a);
                denominator = 2 * dis_a * dis_c;
                cosB = numerator / denominator;
                angle = Math.acos(cosB);
            break;
            case "c":
            case "C":
                numerator = this.Power(2,dis_c) + this.Power(2,dis_a) - this.Power(2,dis_a);
                denominator = 2 * dis_b * dis_a;
                cosC = numerator / denominator;
                angle = Math.acos(cosC);
            break;
        }
        return parseFloat(angle.toFixed(4));
    },

    //找出构成点以形成三个点【找出基准点使得cat、doge与这个构成点形成三角形】
    //refer1、refer2:参照点1、2
    Return_Benchmark (refer1,refer2) {
        var benchmark = cc.p(0,0);
        benchmark.x = refer2.x;
        benchmark.y = refer1.y;
        return benchmark;
    },

    //获取直线方向
    //posA,posB:两点
    Return_Direction(posA,posB) {
        var dir = "";
        var horizontal = "";
        var vertical = "";
        horizontal = this.Direction_Check(posA,posB,"x");
        vertical = this.Direction_Check(posA,posB,"y");
        if(horizontal == "right") {
            dir = "R";    
        }else if(horizontal == "left") {
            dir = "L";
        }
        if (vertical == "top") {
            dir = dir + "T";
        }else if(vertical == "bottom") {
            dir = dir +"B";
        }
        return dir;
    },

    //检测是否到指定位置
    //n_pos:节点位置 a_pos:指定位置 
    Check_Arrive (n_pos,a_pos) {
        var dis = this.Distance_Point(n_pos,a_pos);
        //若当前节点与目标节点直线距离小于10则可视为到达
        if(dis < 10) {
            return true;
        }
        return false;
    },
});
